package com.test.travelinformation.utils;


import android.content.Context;
import android.util.Log;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import rxhttp.wrapper.param.RxHttp;

public class UpImageUtils {

    private List<String> paths;
    private List<UpImage> imgUrls;

    public UpImageUtils() {
        paths = new ArrayList<>();
    }


    public UpImageUtils addImage(String imgPath){
        paths.add(imgPath);
        return this;
    }

    public UpImageUtils addImages(List<String> imgPathList){
        paths.addAll(imgPathList);
        return this;
    }

    public UpImageUtils addImages(String ...imgPaths){
        paths.addAll(Arrays.asList(imgPaths));
        return this;
    }

    public List<UpImage> up(){
        try {
            imgUrls = new ArrayList<>();
            for (String path : paths) {
                File file = new File(path);
                //上传地址
                String url = "https://api.uomg.com/api/image.juejin";
                RxHttp.postForm(url)
                        .add("file","multipart")
                        .addFile("Filedata",file)
                        .asClass(UpImage.class)
                        .subscribe(upImage -> {
                            //成功
                            if (upImage.getCode() == 1){
                                imgUrls.add(upImage);
                            }
                        },throwable -> {
                            //失败
                            Log.e("AAAAAAAAA","失败" + file.toString());
                            imgUrls.add(new UpImage());
                        });
            }
            while (paths.size() > imgUrls.size()){
                Thread.sleep(1000);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return imgUrls;
    }
}
